package msgpack

import (
	"bytes"

	"github.com/vmihailenco/msgpack"
)

type Serialize struct {
	Array bool
}

func Msgpack(array bool) *Serialize {
	return &Serialize{Array: array}
}

func (s *Serialize) Unmarshal(data []byte, v interface{}) (err error) {
	return msgpack.NewDecoder(bytes.NewReader(data)).UseJSONTag(true).Decode(v)
}

func (s *Serialize) Marshal(v interface{}) (b []byte, err error) {
	var buf bytes.Buffer
	err = msgpack.NewEncoder(&buf).UseJSONTag(true).StructAsArray(s.Array).Encode(v)
	return buf.Bytes(), err
}
